<?php

/**
 * albums actions.
 *
 * @package    jobeet
 * @subpackage albums
 * @author     Your name here
 */
class albumsActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->pager = new sfPropelPager( 'Albums', 3 );

    $oCriteria = AlbumsQuery::create()
                                ->filterByIsPublished(true)
                                ->orderById(Criteria::DESC);
                                        
    $this->pager->setCriteria( $oCriteria );  
    $this->pager->setPage( (int) $request->getParameter( 'page', 1 ) );
    $this->pager->init();
    
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->oAlbum = AlbumsPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->oAlbum);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new AlbumsForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new AlbumsForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $Albums = AlbumsQuery::create()->findPk($request->getParameter('id'));
    $this->forward404Unless($Albums, sprintf('Object Albums does not exist (%s).', $request->getParameter('id')));
    $this->form = new AlbumsForm($Albums);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $Albums = AlbumsQuery::create()->findPk($request->getParameter('id'));
    $this->forward404Unless($Albums, sprintf('Object Albums does not exist (%s).', $request->getParameter('id')));
    $this->form = new AlbumsForm($Albums);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $Albums = AlbumsQuery::create()->findPk($request->getParameter('id'));
    $this->forward404Unless($Albums, sprintf('Object Albums does not exist (%s).', $request->getParameter('id')));
    $Albums->delete();

    $this->redirect('albums/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $Albums = $form->save();

      $this->redirect('albums/edit?id='.$Albums->getId());
    }
  }
}
